﻿<?php

class LuyenUser_OrderController extends Zend_Controller_Action
{
	public function checkoutAction()
	{
		
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
		$user = $defaultNamespace->user;
		$cart = $defaultNamespace->carts;
		$defaultNamespace->checkout = true;
		if(($defaultNamespace->step==1) ||((int)($_POST['step_id'])==1)){
			$this->view->step=1;
			if($defaultNamespace->error){
				$this->view->error = $defaultNamespace->error;
				$defaultNamespace->error=null;
			}
			//unset($defaultNamespace->step);
		}
		//var_dump($user->u_email);
		if($cart && $user){
			$this->view->user=$user;
			$this->view->carts= $cart;
			$this->view->tongtien = $defaultNamespace->tongtien;
		}
	}
	
	public function billingAction()
	{
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
		if($_POST['selectShippingMethod']){
			$defaultNamespace->shipping = (int)($_POST['selectShippingMethod']);
		}else{
			$defaultNamespace->error = "Quý khách phải chọn phương thức giao hàng";
			$defaultNamespace->step=1;
			header('Location: http://shoppingcartcnpm.orgfree.com/index.php/LuyenUser/order/checkout');
		}
		if ((int)($_POST['step_id'])==2){
			$this->view->step = 2;	
		}
	}
	public function orderAction(){
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
		if($_POST['selectBillingMethod']){
			$defaultNamespace->billing = (int)($_POST['selectBillingMethod']);
		}else{
			$defaultNamespace->error = "Quý khách phải chọn phương thức thanh toán";
			$defaultNamespace->step=2;
			header('Location: http://shoppingcartcnpm.orgfree.com/index.php/LuyenUser/order/shipping');
		}
		if ((int)($_POST['step_id'])==3){
			$this->view->step = 3;
			$orderModel = new Model_Order();
			$orderDetail = new Model_Orderdetail();
			
			$u_id = $defaultNamespace->user->u_id;
			$pay_id = $defaultNamespace->billing;
			$ship_id = $defaultNamespace->shipping;
			$o_dathanhtoan=0;
			$o_danhanhang=0; 
			$row=$orderModel->createOrder ($u_id,$pay_id,$ship_id,$o_dathanhtoan,$o_danhanhang);
			//var_dump(time());
			$products = $defaultNamespace->carts;
			foreach ($products as $x){
				$orderDetail->createOrderdetail($x['p_id'],$row->o_id,$x['soluong'],$x['gia']);
				//var_dump($x);   
			}
			$defaultNamespace->carts=null;
			//echo"----------------------------da thanh toan thanh cong-------------------------------------";
		}	
	}

}
 
